﻿#include "sqldao.h"

#include <QSqlDatabase>
#include <QSqlQuery>

SqlDao::SqlDao()
{

}

/*
 * @brief:
 * @detail:
 */
bool SqlDao::insertUser(const QString& username, const QString& password)
{
    // 1. 打开数据库
    if(SingleTon<SqlDaoDb>::getReference().open() == false) return false;

    // 2. 语句
    QSqlQuery query;
    query.prepare("insert into client(username, password)"
                  "values(:1, :2);");
    query.bindValue(":1", username);
    query.bindValue(":2", password);

//    if(query.exec() == false){
    if(query.exec() == false){
        qDebug() <<"query.lastError():"<< query.lastError().text();
        return false;
    }else{}

    return true;
}

/*
 * @brief:
 * @detail:
 */
QString SqlDao::selectUser(const QString& username)
{
    // 1. 打开数据库
    if(SingleTon<SqlDaoDb>::getReference().open() == false) return {};

    // 2. 语句
    QSqlQuery query;
    query.prepare("select password from client where username = :1;");
    query.bindValue(":1", username);

    if(query.exec() == false){
        qDebug() << query.lastError().text();
        return {};
    }else{
        if(query.next()){
            return query.value(0).toString();
        }
    }

    return {};
}
